package com.zenithmind.mall.controller;

import com.zenithmind.common.annotation.Log;
import com.zenithmind.common.annotation.RequireAuth;
import com.zenithmind.common.constant.OperationTypeEnum;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import com.zenithmind.mall.pojo.dto.ProductDTO;
import com.zenithmind.mall.pojo.dto.StatusUpdateRequest;
import com.zenithmind.mall.pojo.query.ProductQuery;
import com.zenithmind.mall.pojo.vo.ProductVO;
import com.zenithmind.mall.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品控制器
 * 
 * @author ZenithMind Team
 * @since 2025-06-09
 */
@Slf4j
@RestController
@RequestMapping("/api/zenithMind/mall/product")
@RequiredArgsConstructor
@Tag(name = "商品管理", description = "商品相关接口")
public class ProductController {

    private final ProductService productService;

    @GetMapping("/page")
    @Operation(summary = "分页查询商品列表", description = "支持多条件查询和排序")
    public Result<PageResult<ProductVO>> getProductPage(@Validated ProductQuery query) {
        log.info("分页查询商品列表: {}", query);
        PageResult<ProductVO> result = productService.getProductPage(query);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询商品详情", description = "根据商品ID查询详细信息")
    @Parameter(name = "id", description = "商品ID", required = true)
    public Result<ProductVO> getProductById(@PathVariable String id) {
        log.info("查询商品详情: {}", id);
        ProductVO result = productService.getProductById(id);
        return Result.success(result);
    }

    @PostMapping
    @Operation(summary = "创建商品", description = "创建新商品")
    @RequireAuth(admin = true)
    @Log(description = "创建商品", operationType = OperationTypeEnum.ADD)
    public Result<String> createProduct(@Validated @RequestBody ProductDTO productDTO) {
        log.info("创建商品: {}", productDTO.getName());
        String productId = productService.createProduct(productDTO);
        return Result.success(productId);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新商品", description = "更新商品信息")
    @Parameter(name = "id", description = "商品ID", required = true)
    @RequireAuth(admin = true)
    @Log(description = "更新商品", operationType = OperationTypeEnum.UPDATE)
    public Result<Boolean> updateProduct(@PathVariable String id, 
                                       @Validated @RequestBody ProductDTO productDTO) {
        log.info("更新商品: {}", id);
        Boolean result = productService.updateProduct(id, productDTO);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除商品", description = "删除指定商品")
    @Parameter(name = "id", description = "商品ID", required = true)
    @RequireAuth(admin = true)
    @Log(description = "删除商品", operationType = OperationTypeEnum.DELETE)
    public Result<Boolean> deleteProduct(@PathVariable String id) {
        log.info("删除商品: {}", id);
        Boolean result = productService.deleteProduct(id);
        return Result.success(result);
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除商品", description = "批量删除多个商品")
    @RequireAuth(admin = true)
    @Log(description = "批量删除商品", operationType = OperationTypeEnum.DELETE)
    public Result<Boolean> deleteProducts(@RequestBody List<String> ids) {
        log.info("批量删除商品: {}", ids);
        Boolean result = productService.deleteProducts(ids);
        return Result.success(result);
    }

    @PutMapping("/{id}/on-shelf")
    @Operation(summary = "上架商品", description = "将商品设置为上架状态")
    @Parameter(name = "id", description = "商品ID", required = true)
    @RequireAuth(admin = true)
    @Log(description = "上架商品", operationType = OperationTypeEnum.UPDATE)
    public Result<Boolean> onShelf(@PathVariable String id) {
        log.info("上架商品: {}", id);
        Boolean result = productService.onShelf(id);
        return Result.success(result);
    }

    @PutMapping("/{id}/off-shelf")
    @Operation(summary = "下架商品", description = "将商品设置为下架状态")
    @Parameter(name = "id", description = "商品ID", required = true)
    @RequireAuth(admin = true)
    @Log(description = "下架商品", operationType = OperationTypeEnum.UPDATE)
    public Result<Boolean> offShelf(@PathVariable String id) {
        log.info("下架商品: {}", id);
        Boolean result = productService.offShelf(id);
        return Result.success(result);
    }

    @PutMapping("/{id}/status")
    @Operation(summary = "更新商品状态", description = "更新指定商品的状态")
    @Parameter(name = "id", description = "商品ID", required = true)
    @RequireAuth(admin = true)
    @Log(description = "更新商品状态", operationType = OperationTypeEnum.UPDATE)
    public Result<Boolean> updateProductStatus(@PathVariable String id,
                                             @RequestBody StatusUpdateRequest request) {
        log.info("更新商品状态: {} -> {}", id, request.getStatus());
        Boolean result = productService.updateProductStatus(id, request.getStatus());
        return Result.success(result);
    }

    @GetMapping("/recommend")
    @Operation(summary = "查询推荐商品", description = "获取推荐商品列表")
    @Parameter(name = "limit", description = "限制数量", required = false)
    public Result<List<ProductVO>> getRecommendProducts(@RequestParam(defaultValue = "10") Integer limit) {
        log.info("查询推荐商品，限制数量: {}", limit);
        List<ProductVO> result = productService.getRecommendProducts(limit);
        return Result.success(result);
    }

    @GetMapping("/hot")
    @Operation(summary = "查询热销商品", description = "获取热销商品列表")
    @Parameter(name = "limit", description = "限制数量", required = false)
    public Result<List<ProductVO>> getHotProducts(@RequestParam(defaultValue = "10") Integer limit) {
        log.info("查询热销商品，限制数量: {}", limit);
        List<ProductVO> result = productService.getHotProducts(limit);
        return Result.success(result);
    }

    @GetMapping("/new")
    @Operation(summary = "查询新品", description = "获取新品列表")
    @Parameter(name = "limit", description = "限制数量", required = false)
    public Result<List<ProductVO>> getNewProducts(@RequestParam(defaultValue = "10") Integer limit) {
        log.info("查询新品，限制数量: {}", limit);
        List<ProductVO> result = productService.getNewProducts(limit);
        return Result.success(result);
    }

    @PostMapping("/{id}/view")
    @Operation(summary = "增加商品浏览量", description = "记录商品浏览")
    @Parameter(name = "id", description = "商品ID", required = true)
    public Result<Void> increaseViewCount(@PathVariable String id) {
        productService.increaseViewCount(id);
        return Result.success();
    }

    @GetMapping("/{id}/stock")
    @Operation(summary = "检查商品库存", description = "检查指定数量的库存是否充足")
    @Parameter(name = "id", description = "商品ID", required = true)
    @Parameter(name = "quantity", description = "需要的数量", required = true)
    public Result<Boolean> checkStock(@PathVariable String id, @RequestParam Integer quantity) {
        Boolean result = productService.checkStock(id, quantity);
        return Result.success(result);
    }
}
